import { createStore, applyMiddleware, compose } from 'redux';
import thunkMiddleware from 'redux-thunk';
import { createLogger } from 'redux-logger';
import rootReducer from '../reducers';

import fetchMiddleware from './fetchMiddleware';

const loggerMiddleware = createLogger()

export default function configureStore(preloadedState) {
    return createStore(
        rootReducer,
        preloadedState,
        compose(
            applyMiddleware(
                thunkMiddleware, // 允许我们 dispatch() 函数
                loggerMiddleware, // 一个很便捷的 middleware，用来打印 action 日志
                fetchMiddleware
            ),
            window.devToolsExtension ? window.devToolsExtension() : f => f
        )
    )
}